<template>
  <div v-loading="loading" element-loading-text="加载中">
    <!-- tab区 -->
    <el-tabs v-model="tabActive" @tab-click="doChangeTab">
      <template v-for="(val, key) in dict.platformType">
        <el-tab-pane v-if="key != 99" :key="key" :label="val" :name="key"> </el-tab-pane>
      </template>
    </el-tabs>
    <!-- 表单区 -->
    <el-form :model="configForm" ref="configForm" class="mar-l-20">
      <!-- <el-form-item>
        <div>
          <el-checkbox v-model="configForm.autoSyncFlag" true-label="1" false-label="0">开启订单自动同步</el-checkbox>
          <span class="font-red">（注意：授权已过期的店铺忽略不自动同步，需要手动同步）</span>
        </div>
        <div class="item-box">
          <div>每隔&nbsp;<el-input v-model="configForm.syncFreq" class="width-100"></el-input>&nbsp;分钟自动同步一次订单</div>
          <div class="mar-l-30">（不要小于10分钟，推荐30分钟）</div>
        </div>
      </el-form-item> -->
      <el-form-item>
        <div>
          <el-checkbox v-model="configForm.autoGenerateFlag" true-label="1" false-label="0">开启订单自动生成</el-checkbox>
          <span class="font-red">（注意：授权已过期的店铺忽略不自动同步，需要手动同步）</span>
        </div>
        <div class="item-box">
          <div>
            <el-checkbox v-model="configForm.buyerWordsFlag" true-label="1" false-label="0">有买家留言订单不生成</el-checkbox>
            <el-checkbox v-model="configForm.sellerWordsFlag" true-label="1" false-label="0">有卖家备注订单不生成</el-checkbox>
          </div>
          <div><el-checkbox v-model="configForm.keywordsFlag" true-label="1" false-label="0">开启订单地址过滤</el-checkbox></div>
          <div>地址包含关键字<el-input v-model="configForm.keywords" class="width-300"></el-input>的订单不生成</div>
          <div class="mar-l-30 font-gray">（多个关键字使用“|”隔开）</div>
          <div>包含单品数量大于<el-input v-model="configForm.goodsNumFilter" @keyup.native="configForm.goodsNumFilter=checkInputNum(configForm.goodsNumFilter)" maxlength=4 class="width-300"></el-input>的订单不生成</div>
          <div class="mar-l-30 font-gray">（为空表示该条件不起作用）</div>
        </div>
      </el-form-item>
      <el-form-item v-if="tabActive == 1">
        <div>
          <el-checkbox v-model="configForm.changeAddressAuditFlag" true-label="1" false-label="0" @change="doChangeAduit">买家修改收货信息需要审核</el-checkbox>
          <span class="font-red">（奇门仓/淘宝联盟发货订单默认拒绝修改申请）</span>
        </div>
        <div class="item-box">
          <div><el-checkbox v-model="configForm.deliveryOrderAutoAgreeFlag" true-label="1" false-label="0" @change="doChangeAutoAgree">待发货订单自动同意修改</el-checkbox></div>
        </div>
      </el-form-item>
    </el-form>
    <!-- 底部按钮区 -->
    <div slot="footer" class="dialog-footer mar-b-10">
      <el-button @click="doClose">取消</el-button>
      <el-button type="primary" @click="doSave">保存</el-button>
    </div>
  </div>
</template>

<script>
  import { getDictByGroupName } from '@/api/common/common'
  import { getByCondition, save } from '@/api/goods/thirdOrderConfig'

  export default {
    props: {
      showFlag: {
        type: Boolean,
        default() {
          return false
        }
      }
    },
    watch: {
      showFlag(val) {
        if (val) {
          this.init()
          this.doChangeTab()
        }
      }
    },
    data() {
      return {
        // 数据字典
        dict: {
          platformType: {}
        },
        // tab model
        tabActive: '1',
        // 同步设置表单
        configForm: {
          enterpriseId: '',
          memberId: '',
          platformType: '',
          shopType: '',
          autoSyncFlag: '0',
          syncFreq: '15',
          autoGenerateFlag: '0',
          buyerWordsFlag: '0',
          sellerWordsFlag: '0',
          keywordsFlag: '0',
          goodsNumFilter: '',
          keywords: '',
          changeAddressAuditFlag: '0',
          deliveryOrderAutoAgreeFlag: '0'
        },
        loading: false
      }
    },
    created() {
      // 获取数据字典
      getDictByGroupName('ThirdOrderDTO').then(response => {
        this.dict = response.data
      })
      this.init()
      this.doChangeTab()
    },
    methods: {
      // 初始化
      init() {
        const originData = this.$options.data()
        this.configForm = originData.configForm
        this.configForm.enterpriseId = this.getEnterpriseId()
        this.configForm.memberId = this.getMemberId()
        this.configForm.shopType = this.getMemberId() ? 1 : 2
      },
      // 平台切换事件
      doChangeTab() {
        this.loading = true
        this.configForm.platformType = this.tabActive
        const form = {
          enterpriseId: this.configForm.enterpriseId,
          memberId: this.configForm.memberId,
          platformType: this.configForm.platformType,
          shopType: this.configForm.shopType
        }
        getByCondition(form).then(response => {
          this.loading = false
          if (response.isError) return
          const data = response.data
          if (data) {
            Object.assign(this.configForm, data)
            this.configForm.autoSyncFlag = data.autoSyncFlag.toString()
            this.configForm.autoGenerateFlag = data.autoGenerateFlag.toString()
            this.configForm.buyerWordsFlag = data.buyerWordsFlag.toString()
            this.configForm.sellerWordsFlag = data.sellerWordsFlag.toString()
            this.configForm.keywordsFlag = data.keywordsFlag.toString()
            this.configForm.changeAddressAuditFlag = data.changeAddressAuditFlag.toString()
            this.configForm.deliveryOrderAutoAgreeFlag = data.deliveryOrderAutoAgreeFlag.toString()
          } else {
            this.init()
          }
        })
      },
      // 审核修改事件
      doChangeAduit(val) {
        this.configForm.deliveryOrderAutoAgreeFlag = '0'
      },
      // 自动同意修改事件
      doChangeAutoAgree(val) {
        if (val === '1' && this.configForm.changeAddressAuditFlag !== '1') {
          this.$message.warning('请先选择是否需要审核')
          this.configForm.deliveryOrderAutoAgreeFlag = '0'
        }
      },
      // 同步设置保存事件
      doSave() {
        this.configForm.platformType = this.tabActive
        if (!this.configForm.platformType) {
          this.$message.error('平台类型不能为空')
          return
        }
        if (!this.configForm.shopType) {
          this.$message.error('店铺类型不能为空')
          return
        }
        if (this.configForm.autoSyncFlag === '1') {
          if (!this.configForm.syncFreq || this.configForm.syncFreq * 1 < 10) {
            this.$message.error('同步订单间隔时间不能小于10分钟')
            return
          }
          if (this.configForm.syncFreq * 1 > 60) {
            this.$message.error('同步订单间隔时间不能大于60分钟')
            return
          }
        }
        this.loading = true
        save(this.configForm).then(response => {
          if (response.isError) {
            this.loading = false
            return
          }
          this.$message({ message: '保存成功', type: 'success' })
          this.doChangeTab()
        })
      },
      // 取消事件
      doClose() {
        this.$emit('callback')
      }
    }
  }
</script>

<style lang="less" scoped>
  .item-box {
    line-height: 2.3; 
    background-color: #f2f2f2;
    padding-left: 20px;
    padding-top: 5px;
    width: 96%;
  }
</style>